home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / misc / ClipWatch.lha / ClipWatch / Sources / misc.c < prev    next >
C/C++ Source or Header  |  2000-12-29  |  2KB  |  109 lines

  1. /* ---------------------------- */
  2. /* Functions to access ini file */
  3. /* By MaVaTi, for ClipWatch     */
  4. /* ---------------------------- */
  5.  
  6. int ReadStringParamIni(char * Search,char * Def,char * Res,char * IniFile)
  7. {
  8.     FILE * File;
  9.    char Line[255];
  10.    char * NotFin;
  11.    int Fnd=FALSE;
  12.    File=fopen(IniFile,"rt");
  13.    if (File)
  14.    {
  15.         do
  16.        {
  17.            NotFin=fgets(Line,255,File);
  18.          if (strncmp(Search,Line,strlen(Search))==0)
  19.          {
  20.              if (Line[strlen(Search)]=='=')
  21.             {
  22.                  Fnd=TRUE;
  23.                 strcpy(Res,&Line[strlen(Search)+1]);
  24.               Res[strlen(Res)-1]='\0';
  25.             }
  26.          }
  27.        }
  28.        while (NotFin&&(!Fnd));
  29.       fclose(File);
  30.    }
  31.    if (!Fnd)
  32.        strcpy(Res,Def);
  33.    return Fnd;
  34. }
  35.  
  36. long ReadIntParamIni(char * Search,long Def,char * IniFile)
  37. {
  38.     char tmp[50];
  39.    if (ReadStringParamIni(Search,"",tmp,IniFile))
  40.        return atol(tmp);
  41.    else
  42.        return Def;
  43. }
  44.  
  45.  
  46. int WriteStringParamIni(char * Search,char * NewVal,char * IniFile)
  47. {
  48.     FILE * OldFile;
  49.     FILE * NewFile;
  50.     char OldIniFile[255];
  51.     char Line[255];
  52.     char * NotFin;
  53.     int Fnd=FALSE;
  54.     strcpy(OldIniFile,IniFile);
  55.     strcat(OldIniFile,".bak");
  56.     rename(IniFile,OldIniFile);
  57.     NewFile=fopen(IniFile,"wt");
  58.     if (NewFile)
  59.     {
  60.     
  61.         OldFile=fopen(OldIniFile,"rt");
  62.         if (OldFile)
  63.         {
  64.             do
  65.            {
  66.                 NotFin=fgets(Line,255,OldFile);
  67.                 if (strncmp(Search,Line,strlen(Search))==0)
  68.                 {
  69.                     if (Line[strlen(Search)]=='=')
  70.                     {
  71.                         Fnd=TRUE;
  72.                         strcpy(&Line[strlen(Search)+1],NewVal);
  73.                         strcat(Line,"\n");
  74.                     }
  75.                 }
  76.                 if(NotFin)
  77.                 {
  78.                     fputs(Line,NewFile);
  79.                 }
  80.            }
  81.            while (NotFin);
  82.             fclose(OldFile);
  83.  
  84.         }
  85.  
  86.         if (!Fnd)
  87.         {
  88.             strcpy(Line,Search);
  89.             strcat(Line,"=");
  90.             strcat(Line,NewVal);
  91.             strcat(Line,"\n");
  92.             fputs(Line,NewFile);
  93.         }
  94.  
  95.         fclose(NewFile);
  96.     }
  97.     remove(OldIniFile);
  98.     return Fnd;
  99. }
  100.  
  101. long WriteIntParamIni(char * Search,long NewVal,char * IniFile)
  102. {
  103.     char tmp[50];
  104.     int Fnd;
  105.     sprintf(tmp,"%u",NewVal);
  106.     Fnd=WriteStringParamIni(Search,tmp,IniFile);
  107.     return Fnd;
  108. }
  109.